import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;

import util.Display;
import util.ThreadUtil;
import ai.GameSolover;
import ai.MouseOperator;
import ai.ScreenImage;
import ai.ScreenImgBinBorder;
import ai.ScreenImgBinWater;
import ai.ScreenImgBinary;

public class Main
{
	public static void main(String [] args)
	{
		do
		{
			try
			{
				Main.tenWater();
			}catch(Exception e)
			{
				System.out.println("本次识别错误，正在重试");
			}
			ThreadUtil.sleep(3500);
		}while(true);
	}
	
	public static void tenWater() throws IOException
	{
		BufferedImage screenImg = ScreenImage.screenShotImage();
		if(screenImg == null)
		{
			return;
		}
		BufferedImage borderImg = ScreenImgBinary.getBorderImg(screenImg);
		BufferedImage waterImg = ScreenImgBinary.getWaterImg(screenImg);
		ImageIO.write(screenImg, "png", new File("D:/tenWater/screenImg.png"));
		ImageIO.write(borderImg, "png", new File("D:/tenWater/borderImg.png"));
		ImageIO.write(waterImg, "png", new File("D:/tenWater/waterImg.png"));
		float [] heightInfo = ScreenImgBinBorder.getHeightInfo(borderImg);//横真线信息
		boolean [] heightNeedLines = ScreenImgBinBorder.getNeedLines(heightInfo);
		float [] widthInfo = ScreenImgBinBorder.getWidthInfo(borderImg);//竖直线信息
		boolean [] widthNeedLines = ScreenImgBinBorder.getNeedLines(widthInfo);
		BufferedImage lineImg = ScreenImgBinBorder.toLineImg(widthNeedLines, heightNeedLines, borderImg.getWidth(), borderImg.getHeight());
		ImageIO.write(lineImg, "png", new File("D:/tenWater/lineImg.png"));
		int [] useHeightLinePos = ScreenImgBinBorder.getUseLines(heightNeedLines);//水平线
		int [] useWidthLinePos = ScreenImgBinBorder.getUseLines(widthNeedLines);//垂直线
		int [] equalHeightPos = ScreenImgBinBorder.getEqualDistacePoint(useHeightLinePos);//等间距水平线的坐标
		int [] equalWidthPos = ScreenImgBinBorder.getEqualDistacePoint(useWidthLinePos);//等间距垂直线的坐标
		BufferedImage equalDistanceLineImg = ScreenImgBinBorder.toEqualDistanceLineImg(equalWidthPos, equalHeightPos, borderImg.getWidth(), borderImg.getHeight());
		ImageIO.write(equalDistanceLineImg, "png", new File("D:/tenWater/equalDistanceLineImg.png"));
		BufferedImage cleanedWaterImg = ScreenImgBinWater.getCleanWaterImg(waterImg, equalHeightPos[0], equalHeightPos[equalHeightPos.length - 1], equalWidthPos[0],
				equalWidthPos[equalWidthPos.length - 1]);
		ImageIO.write(cleanedWaterImg, "png", new File("D:/tenWater/cleanedWaterImg.png"));
		float [][] imgInfo = ScreenImgBinWater.getWaterPercent(waterImg, equalHeightPos, equalWidthPos);
		int [][] waterState = ScreenImgBinWater.getWaterState(imgInfo);
		Display.sysout(waterState);
		GameSolover gs = new GameSolover(waterState);
		gs.solove();
		int most = gs.getMostValue();
		int [] comboNum = gs.getMostValueCombo();
		int [][] action = gs.getMostValueAction();
		int [][] lastState = gs.getMostValueState();
		System.out.println("采用方法：");
		Display.sysout(action);
		System.out.println("消去细节：");
		Display.sysout(comboNum);
		System.out.println("最终收益：" + most);
//		System.out.println("最终状态：");
//		Display.sysout(lastState);
//		MouseOperator.clickOn(action, comboNum, equalWidthPos, equalHeightPos);
		if(most >= 0)
		{
			MouseOperator.clickOn(action, comboNum, equalWidthPos, equalHeightPos);
		}else
		{
			MouseOperator.clickOn(action[0][1], action[0][0], equalWidthPos, equalHeightPos);
		}
		System.out.println("============================================================");
	}
}
